package com.copasso.cocobill.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import com.copasso.cocobill.R;
import com.copasso.cocobill.common.Constants;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import java.io.IOException;

/**
 * Created by zhouas666 on 2017/12/27.
 */
public class ThemeManager {

    private String[] mThemes = { "原谅绿", "酷炫黑", "少女红", "胖次蓝", "基佬紫", "活力橙", "大地棕" };

    private static ThemeManager instance;

    public static ThemeManager getInstance() {
        if (instance == null) {
            instance = new ThemeManager();
        }
        return instance;
    }

    public String[] getThemes() {
        return mThemes;
    }

    /**
     * 设置主题色
     * 
     * @param context activity
     * @param theme   主题名称
     */
    public void setTheme(Activity context, String theme) {
        String curTheme = SharedPUtils.getCurrentTheme(context);//这里会自动返回原谅绿
        if (curTheme != null && curTheme.equals(theme)) {//将选中的颜色与原谅绿对比
            return;
        }

        SharedPUtils.setCurrentTheme(context, theme);

        context.finish();
        Intent intent = context.getIntent();
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Constants.EXTRA_IS_UPDATE_THEME, true);
        context.startActivity(intent);
    }

    /**
     * 获取当前主题名
     * 
     * @param context 上下文
     * @return 如: 少女粉
     */
    public String getCurThemeName(Context context) {
        return SharedPUtils.getCurrentTheme(context);
    }

    public void init(Context context) {
        String theme = SharedPUtils.getCurrentTheme(context);
        if (theme.equals(mThemes[0])) {
            context.setTheme(R.style.AppTheme);
        } else if (theme.equals(mThemes[1])) {
            context.setTheme(R.style.AppTheme_Black);
        } else if (theme.equals(mThemes[2])) {
            context.setTheme(R.style.AppTheme_Green);
        } else if (theme.equals(mThemes[3])) {
            context.setTheme(R.style.AppTheme_Blue);
        } else if (theme.equals(mThemes[4])) {
            context.setTheme(R.style.AppTheme_Purple);
        } else if (theme.equals(mThemes[5])) {
            context.setTheme(R.style.AppTheme_Orange);
        } else if (theme.equals(mThemes[6])) {
            context.setTheme(R.style.AppTheme_Brown);
        }
    }

    /**
     * 设置背景图片
     * 
     * @param context  上下文
     * @param imageUri 图片的Uri
     */
    public void setBackgroundImage(Activity context, Bitmap bitmap) {
        Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
        CoordinatorLayout mainLayout = context.findViewById(R.id.main_cl);
        mainLayout.setBackground(drawable);

        // 如果需要保存裁剪后的图片 URI，可以将其存储到 SharedPreferences
        // Uri imageUri = ... // 将 bitmap 转换为 Uri 并存储
        // SharedPUtils.setBackgroundImageUri(context, imageUri.toString());
    }
    /**
     * 获取背景图片
     * 
     * @param context 上下文
     * @return 背景图片的Bitmap
     */
    public Bitmap getBackgroundImage(Context context) {
        String imageUriString = SharedPUtils.getBackgroundImageUri(context);
        if (imageUriString != null) {
            try {
                Uri imageUri = Uri.parse(imageUriString);
                return MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUri);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
